
#include "stdint.h"
#include "bsp_board.h"
#include "driver_ctp816s.h"
#include "simulate_i2c.h"
#include "platform.h"
#include "app.h"

#ifdef __TOUCH_CTP816S__
uint8_t touch_interrupt_flag = 0; // touch flag 
uint8_t tp_touch_region = 0;
extern uint8_t long_press_flag;
extern Device_State_Def my_device;

void BSP_TP_Drive_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    EXTI_InitTypeDef EXTI_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;

    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, ENABLE);
	
    RCC_APB2PeriphClockCmd(RCC_APB2ENR_SYSCFG, ENABLE);

  #ifndef __MM_SIMULATE_I2C__
	// SDA
	GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = TP_SDA_PIN;
    GPIO_InitStruct.GPIO_Mode = TP_SDA_MODE;	
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(TP_SDA_PORT, &GPIO_InitStruct);

    // SCL
   // GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = TP_SCL_PIN;
    GPIO_InitStruct.GPIO_Mode = TP_SCL_MODE;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(TP_SCL_PORT, &GPIO_InitStruct);
	
    SCL_HIGH();
	SDA_HIGH();
 #endif

    // RESET
   // GPIO_StructInit(&GPIO_InitStruct);
	GPIO_InitStruct.GPIO_Pin = TP_RESET_PIN;
    GPIO_InitStruct.GPIO_Mode = TP_RESET_MODE;	
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(TP_RESET_PORT, &GPIO_InitStruct);	

	TP_RES_SET();

	/* TP IRQ ->EXTI_Line0 */
    //GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin  = TP_IRQ_PIN;
    GPIO_InitStruct.GPIO_Mode = TP_IRQ_MODE;
    GPIO_Init(TP_IRQ_PORT, &GPIO_InitStruct);

    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

    EXTI_StructInit(&EXTI_InitStruct);
    EXTI_InitStruct.EXTI_Line    = EXTI_Line0;
    EXTI_InitStruct.EXTI_Mode    = EXTI_Mode_Interrupt;
    EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling; // EXTI_Trigger_Rising
    EXTI_InitStruct.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStruct);


	/* EXTI Interrupt */
    NVIC_InitStruct.NVIC_IRQChannel = EXTI0_1_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);

	

}
////////////////////////////////////////
void driver_ctp816s_reset(void)
{

   TP_RES_CLR();
   DelayMs(10);
   TP_RES_SET();
   DelayMs(50);
}
////////////////////////////////////////
void driver_touch_ctp816s_init(void)
{
	uint8_t fw_version = 0xFF;	
	uint8_t chip_id= 0xFF;
	uint8_t mode=0;
	
    BSP_TP_Drive_Init();
	#ifdef __MM_SIMULATE_I2C__
	I2C_Configure();
	#endif
    DelayMs(50);
    driver_ctp816s_reset();

	//sda_control_10();
	//scl_control_10();
	//scl_sda_change();
  #if 1
   for(uint8_t i=0;i<2;i++)
   {
    driver_touch_ctp816s_get_chip_id(&chip_id);
		 if(chip_id==CTP_DEV_ID_816S)
	 	{
	 	 break;
	 	}
   }
  #if 1
   DelayMs(1); 
  for(uint8_t i=0;i<2;i++)
   { 
     driver_touch_ctp816s_get_fw_version(&fw_version);
   	 if(fw_version==FIRMWARE_VERSION_816S)
	 {
	 	 break;
	 }
   }
  if((chip_id != CTP_DEV_ID_816S)||(fw_version != FIRMWARE_VERSION_816S))  // upgrade code
  	{

	    ctp_hynitron_update();

  	}
  else{  // set touch modle

    // set for report dot mode
    mode = CTP_REPORT_GESTRURE_MODE_CMD;
    I2C_MasterWrite((APP_SLAVE<<1),CTP_MODE_REG,&mode,1);



  	}
  #endif
  #endif
}



void driver_touch_ctp816s_read(uint8_t reg, uint8_t *p_data,uint8_t num)
{

	I2C_read(APP_SLAVE<<1,reg,p_data,num);
	//I2C_RxData_Polling(APP_SLAVE,reg,p_data,1);
}

void driver_touch_ctp816s_get_chip_id(uint8_t *p_chip_id)
{
    driver_touch_ctp816s_read(CTP_DEV_ID_ADDR, p_chip_id,1); 
	//driver_touch_ctp816s_read(0x00, p_chip_id); 
   // APP_PRINT_INFO1("[cst0x6 get chip id] -- 0x%X", *p_chip_id);
   UART2_SendMgs(p_chip_id,1);
}

void driver_touch_ctp816s_get_fw_version(uint8_t *p_fw_version)
{
      driver_touch_ctp816s_read(CTP_VER_ADDR, p_fw_version,1);
   // APP_PRINT_INFO1("[cst0x6 get fw version] -- 0x%X", *p_fw_version);
   UART2_SendMgs(p_fw_version,1);
}
/////////////////////////////////////////////////////////////
void had_touch_interrupt_get_data(void)
{
   uint8_t tp_temp[8];
   uint8_t gestrue_id,touch_type;
   uint16_t touch_x,touch_y;
//   uint8_t tp_temp_test[8];

 
   if(touch_interrupt_flag)
   	{
      touch_interrupt_flag = 0;

     driver_touch_ctp816s_read(0,&tp_temp[0],7);

	 //05 one touch, 0B double touch,0C long touch,AA big figer 
	 if(tp_temp[1] == 0x05)
	 {
	   touch_x = ((uint16_t)(tp_temp[3] & 0x0F)<<8) + (uint16_t)tp_temp[4];
       touch_y = ((uint16_t)(tp_temp[5] & 0x0F)<<8) + (uint16_t)tp_temp[6];
       Get_touch_region(touch_x,touch_y);
	  /* 
	   tp_temp_test[0] = 0x5;
	   tp_temp_test[1] = 0x1A;
	   tp_temp_test[2] = '\n';
	   UART2_SendMgs(&tp_temp_test[0],3); 
	   UART2_SendMgs(&tp_temp[3],4); */
	 }
	 else if(tp_temp[1] == 0x0B)
	 {
	   touch_x = ((uint16_t)(tp_temp[3] & 0x0F)<<8) + (uint16_t)tp_temp[4];
       touch_y = ((uint16_t)(tp_temp[5] & 0x0F)<<8) + (uint16_t)tp_temp[6];
     /*
	   tp_temp_test[0] = 0x0b;
	   tp_temp_test[1] = 0x1A;
	    tp_temp_test[2] = '\n';
	   	   UART2_SendMgs(&tp_temp_test[0],3); 
	   UART2_SendMgs(&tp_temp[3],4); 
	    */
	 } 
	 else if(tp_temp[1] == 0x0C)
	 {
	   touch_x = ((uint16_t)(tp_temp[3] & 0x0F)<<8) + (uint16_t)tp_temp[4];
       touch_y = ((uint16_t)(tp_temp[5] & 0x0F)<<8) + (uint16_t)tp_temp[6];
        long_press_flag = 1;
	    Get_touch_region(touch_x,touch_y);
     /*
	    tp_temp_test[0] = 0x0c;
		tp_temp_test[1] = 0x1A;
		 tp_temp_test[2] = '\n';
		UART2_SendMgs(&tp_temp_test[0],3); 
	   UART2_SendMgs(&tp_temp[3],4); 
	    */
	 }
	 else{
      ;
	 }

     UART2_SendMgs(&tp_temp[0],7); 
	
   	}

}
////////////////////////////////////////////////////

void Get_touch_region(uint16_t x,uint16_t y)
{

  if((x>1)&&(x<(LCD_WIDTH-2))&&(y>BATTERY_HEIGH)&&(y<(BATTERY_HEIGH+CLOCK_HEIGH)))  // 2, time region
  	{
      my_device.tp_touch_region = 2;
  	}
  else if((x>(2+CHINESE_CHAR_HEIGH*2+8))&&(x<(LCD_WIDTH-2))&&(y>(BATTERY_HEIGH+2*GAP_HEIGH+CLOCK_HEIGH)&&(y<(BATTERY_HEIGH+2*GAP_HEIGH+CLOCK_HEIGH+23))))  // 3, strength region
  	{
       my_device.tp_touch_region = 3;
  	}
  else if((x>(CHINESE_CHAR_WIDTH*2))&&(x<(LCD_WIDTH-2))&&(y>(BATTERY_HEIGH+2*GAP_HEIGH+CLOCK_HEIGH+(23-CHINESE_CHAR_HEIGH)+23))&&(y<(BATTERY_HEIGH+2*GAP_HEIGH+CLOCK_HEIGH+(23-CHINESE_CHAR_HEIGH)+23+CHINESE_CHAR_HEIGH)))  // 4, work mode region
  	{
        my_device.tp_touch_region = 4;
  	}
  else if((x>(CHINESE_CHAR_WIDTH))&&(x<(LCD_WIDTH-2))&&(y>(LCD_HEIGHT-34))&&(y<(LCD_HEIGHT-1))) // 5 press icon region
  	{
        my_device.tp_touch_region = 5;
  	}
  else{
  	    my_device.tp_touch_region = 5;
  	}




}

////////////////////////////////////////////////////
// set tp enter sleep reg
void TP_enable_enter_sleep(uint8_t enable)
{
   uint8_t state;

   if(enable)
   	{
   	 state = 0x00;  // enable sleep 
   	}
   else{
    state = 0x01;  // disenable sleep 
   	}
   
    I2C_MasterWrite((APP_SLAVE<<1),CTP_SLEEP_REG,&state,1);


}





#endif

